var ContactList = new Object();
ContactList.count = 0;
var GroupList = new Object();
GroupList.count = 0;

var Group = Class.create();
Group.prototype = {
	initialize: function()
	{
		this.name = "";
		this.contacts = new Object();
	},
	
	toString: function()
	{
		//TODO: remove ldif
		return this.name;
	}
};


var Contact = Class.create();
Contact.prototype = {
	initialize: function(message)
	{
		this.id = "";
		this.groupName = "";
		this.name_first = "";
		this.name_last = "";
		this.name_nick = "";
		this.birthday = "";
		this.street = "";
		this.post_code = "";
		this.place = "";
		this.state = "";
		this.country = "";
		this.phone_home = "";
		this.phone_mobile = "";
		this.phone_work = "";
		this.mail_first = "";
		this.mail_second = "";
		this.im_jabber = "";
		this.im_icq = "";
		this.homepage = "";
		this.notes = "";
		this.fulltext = "";
	},

	fillFulltext: function()
	{
		this.fulltext = this.name_first
				+' '+this.name_last
				+' '+this.name_nick
				+' '+this.birthday
				+' '+this.street
				+' '+this.post_code
				+' '+this.place
				+' '+this.state
				+' '+this.country
				+' '+this.phone_home
				+' '+this.phone_mobile
				+' '+this.phone_work
				+' '+this.mail_first
				+' '+this.mail_second
				+' '+this.im_jabber
				+' '+this.im_icq
				+' '+this.homepage
				+' '+this.notes
				+' '+this.groupName;
	},
	
	toString: function()
	{
		return this.name_first + ' ' + this.name_last + ' (' + this.mail_first+')'
	},
	
	toDetailList: function()
	{
		var resultContainer = document.createElement('div');
		resultContainer.style.overflow = 'hidden';
		resultContainer.style.height = '100%';
		
		var head = document.createElement('h2');
		head.innerHTML = 'Visitenkarte von ' + this.name_first + ' ' + this.name_last;
		resultContainer.appendChild(head);
		
		var body = document.createElement('div');
		body.id = 'card';
		body.style.overflow = 'auto';
		body.style.height = '100%';
		
		var entries = new Array(
				{title:'Name', content:this.name_first+' '+this.name_last}, 
				{title:'Spitzname', content:this.name_nick}, 
				{title:'Mail', content:this.mail_first},
				{title:'Zus&auml;tzliche Mail', content:this.mail_second}
			);
		var childDiv = this.getDetailBlock('Kontakt', entries, 'leftDetail');
		if (childDiv != undefined)
		{ body.appendChild(childDiv); }
		
		
		entries = new Array(
				{title:'Privat', content:this.phone_home}, 
				{title:'Arbeit', content:this.name_work}, 
				{title:'Handy', content:this.phone_mobile},
				{title:'Fax', content:this.fax}
			);
		childDiv = this.getDetailBlock('Telefon', entries, 'rightDetail');
		if (childDiv != undefined)
		{ body.appendChild(childDiv); }
		
		
		entries = new Array(
				{title:'', content:this.street}, 
				{title:'', content:this.post_code+' '+this.place+' ('+this.state+')'}, 
				{title:'', content:this.country},
				{title:'', content:this.homepage}
			);
		childDiv = this.getDetailBlock('Adresse (Privat)', entries, 'leftDetail');
		if (childDiv != undefined)
		{ body.appendChild(childDiv); }
		
		
		entries = new Array(
				{title:'', content:this.title},
				{title:'', content:this.bureau}, 
				{title:'', content:this.organisation}, 
				{title:'', content:this.street_work}, 
				{title:'', content:this.post_code_work+' '+this.place_work+' ('+this.state_work+')'}, 
				{title:'', content:this.country_work},
				{title:'', content:this.homepage_work}
			);
		childDiv = this.getDetailBlock('Adresse (Arbeit)', entries, 'rightDetail');
		if (childDiv != undefined)
		{ body.appendChild(childDiv); }
		
		var commentBlock = document.createElement('div');
		commentBlock.className = "leftDetail";
		commentBlock.innerHTML = '<h4>Sonstiges</h4>'+this.notes;
		body.appendChild(commentBlock);
		
		resultContainer.appendChild(body);
		
		return resultContainer;
	},
	
	getDetailBlock: function(category, entries, divClass)
	{
		var block = undefined;
		if (entries.length > 0)
		{
			block = document.createElement('div');
			block.className = divClass;
			var heading = document.createElement('h4');
			heading.innerHTML = category;
			block.appendChild(heading);
			var list = document.createElement('ul');

			var item;
			var gotElements = false;
			for (var i=0; i<entries.length; i++)
			{
				item = this.getDetailBlockEntry(entries[i].title, entries[i].content);
				if (item != undefined)
				{
					gotElements = true;
					list.appendChild(item);
				}
			}
			
			block.appendChild(list);
			
			if (!gotElements)
			{
				block = undefined;
			}
		}
		
		return block;
	},
	
	getDetailBlockEntry: function(title, content)
	{
		var entry = undefined;
		
		if (content != undefined && content != null && content != "" && content != 'undefined undefined (undefined)')
		{
			var entry = document.createElement('li');
			if (title != undefined && title != null && title != '')
			{
				entry.innerHTML = title + ': ' + content;
			}
			else
			{
				entry.innerHTML = content;
			}
		}
		
		return entry;
	}
};

function parseData(response)
{
	//$('Debug').innerHTML = response.responseText;
	
	ContactList = new Object();
	ContactList.count = 0;
	GroupList = new Object();
	GroupList.count = 0;

	if (response && response.responseXML)
	{
		var doc = response.responseXML;
		var root = doc.firstChild;

		for (var row in root.childNodes)
		{
			if (root.childNodes[row].nodeName == 'contact')
			{
				var newContact = new Contact();
				
				for (var propertyKey in root.childNodes[row].childNodes)
				{
					var property = root.childNodes[row].childNodes[propertyKey];
					
					if (property.nodeName != '#text')
					{
						newContact[property.nodeName] = property.textContent;
					}
				}
				
				if (!GroupList[newContact.groupName])
				{
					var newGroup = new Group();
					newGroup.name = newContact.groupName
					GroupList[newGroup.name] = newGroup;
					GroupList.count++;
				}
				
				GroupList[newGroup.name].contacts[newContact.id] = newContact;

				newContact.fillFulltext();
				ContactList[newContact.id] = newContact;
				ContactList.count++;
			}
		}
	}

	showData();
}